var fs=require("fs");
var proc=require("process");

exports.add=[{
	 _type:"model_methods",
	_config: {name:"FileFolder"},

	BeforeDeleteCustom: function() {
		var rr=this._SQL().fallback_href2;
		this.tmp_path=rr.F("Files","file_folder_path",this.id,1);
	},

	AfterDeleteCustom: function() {	
		var rr=this._SQL().fallback_href2;
		var dir=new fs.Directory(this.tmp_path);
		if (dir.exists()) {
			try {
				if (dir.exists()) dir.remove();
			} catch (e) {
				throw new Error("Cannot remove directory - '"+e+"', path="+this.tmp_path);
			}
		}
	},

	MoveToFolder: function(fld_id) {
		if (fld_id==this.parent_id) return;
		var rr=this._SQL().fallback_href2;
		var t=this;
		var oldpath=rr.F("Files","file_folder_path",this.id,1);
		this.parent_id=fld_id;
		var newpath=rr.F("Files","file_folder_path",this.parent_id,1);
		
		var p=new proc.Process();
		rr.F("Files","mkdir_rec",newpath);
		p.system("mv "+oldpath+" "+newpath);
	}
}];

